package cn.edu.neosoft.sunbo509.finefood.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neosoft.sunbo509.finefood.Activity.DishesdetailActivity;
import cn.edu.neosoft.sunbo509.finefood.Bean.ShopdetailBean;
import cn.edu.neosoft.sunbo509.finefood.R;

/**
 * Created by 小博 on 2017/10/31.
 */

public class ShopdetailAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<ShopdetailBean>list;
    private LayoutInflater layoutInflater;
    private String userid;
    private String shopname;

    public ShopdetailAdapter(Context context) {
        this.context=context;
        layoutInflater = LayoutInflater.from(context);
    }
    public void setList(List<ShopdetailBean>list){
        this.list=list;
        notifyDataSetChanged();
    }

    public void setUserid(String id){
        this.userid=id;
    }
    public void setShopname(String name){this.shopname=name;}

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shopdetail, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ShopdetailBean shopdetailbean=list.get(position);
       ViewHolder viewHolder= (ShopdetailAdapter.ViewHolder) holder;
        if(shopdetailbean==null) {
            return;
        }
        Picasso.with(context)
                .load(shopdetailbean.getPic())
                .placeholder(null)
                .into(viewHolder.iv_picdetai2);
        viewHolder.tv_foodname2.setText(shopdetailbean.getFoodname());
        viewHolder.tv_intro2.setText(shopdetailbean.getIntro());
        viewHolder.tv_price2.setText((int) shopdetailbean.getPrice()+"元");
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, DishesdetailActivity.class);
                intent.putExtra("foodid",list.get(position).getFood_id());
                intent.putExtra("shopid",list.get(position).getShop_id());
                intent.putExtra("userid",userid);
                intent.putExtra("foodname",list.get(position).getFoodname());
                intent.putExtra("foodprice",list.get(position).getPrice());
                intent.putExtra("shopname",shopname);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if(list==null)
            return 0;
        else
            return list.size();
    }

    public class  ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_price2,tv_foodname2,tv_intro2;
        private ImageView iv_picdetai2;
        public ViewHolder(View itemView) {
            super(itemView);
            tv_foodname2=(TextView)itemView.findViewById(R.id.tv_foodname);
            tv_intro2=(TextView)itemView.findViewById(R.id.tv_intro);
            tv_price2=(TextView)itemView.findViewById(R.id.tv_price);
            iv_picdetai2=(ImageView)itemView.findViewById(R.id.iv_picdetail);
        }
    }

}
